package com.study.fastdfs;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class MyStorageClient extends StorageClient {
	public MyStorageClient(TrackerServer trackerServer, StorageServer storageServer) {
		super(trackerServer, storageServer);
	}

	public String[] uploadSlaveFile(String group_name, String master_filename, String prefix_name,
						   String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException {
		if ((master_filename == null || master_filename.length() == 0) ||
				(prefix_name == null)) {
			throw new MyException("invalid arguement");
		}

		File f = new File(local_filename);
		FileInputStream fis = new FileInputStream(f);

		if (file_ext_name == null) {
			int nPos = local_filename.lastIndexOf('.');
			if (nPos > 0 && local_filename.length() - nPos <= ProtoCommon.FDFS_FILE_EXT_NAME_MAX_LEN + 1) {
				file_ext_name = local_filename.substring(nPos + 1);
			}
		}

		try {
			return this.do_upload_file(ProtoCommon.STORAGE_PROTO_CMD_UPLOAD_SLAVE_FILE, group_name, master_filename, prefix_name,
					file_ext_name, f.length(), new UploadStream(fis, f.length()), meta_list);
		} finally {
			fis.close();
		}
	}
}
